<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ include file="../session.jsp"%>
<!DOCTYPE html>
<html lang="ko">
  <head>
  	<%@ include file="../head.jsp"%>${alert}
  </head>
  <body>
  	<%@ include file="../menu.jsp"%>
	<div class="container">
		<div class="page-header clearfix">
			<h2 class="pull-left">파우치 카테고리 관리</h2>
			<div class="pull-right" style="padding-top: 30px">
				<a href="#" id="opensearch" class="btn btn-info">파우치 카테고리 등록 펼치기/접기</a>
			</div>
		</div>
		
		<div id="searchdiv">
			<div class="clearfix">
				<div class="table-responsive">
					<form id="insertPouchCategory" class="form-horizontal" role="form" method="post" action="listPouchCategory.do">
						<table class="table table-bordered">
							<thead>
								<tr class="success">
									<th class="text-center col-md-5">카테고리명</th>
									<th class="text-center col-md-5">노출순서</th>
									<th class="text-center col-md-2">추가</th>
								</tr>
							</thead>
							<tbody>
								<tr class="text-center">
									<td class="col-md-5"><input type="text" class="form-control" name="pouchCategoryText"/></td>
									<td class="col-md-5"><input type="number" class="form-control" name="sortKey"/></td>
									<td class="col-md-2"><button type="submit" class="btn btn-primary btn-block btn-sm">등록</button></td>
								</tr>
							</tbody>
						</table>
					</form>
				</div>
				<hr/>
			</div>
		</div>

		<div class="table-responsive">
			<span class="pull-left" style="margin-bottom:10px;">등록 : ${page.totalArticleCount}건 / 활성 : ${display}건 / 비활성 : ${unDisplay}</span>
			<table class="table table-striped table-hover">
				<thead>
					<tr class="warning row">
						<th class="text-center col-md-1">노출순서</th>
						<th class="text-center col-md-6">카테고리명</th>
						<th class="text-center col-md-2">파우치수</th>
						<th class="text-center col-md-1">수정</th>
						<th class="text-center col-md-2">
							<c:choose>
								<c:when test="${sort eq 'isDisplay'}"><a href="listPouchCategory.do?order=isUnDisplay">전시여부 ▲</a></c:when>
								<c:otherwise><a href="listPouchCategory.do?order=isDisplay">전시여부 ▼</a></c:otherwise>
							</c:choose>
						</th>
					</tr>
				</thead>
				<tbody>
					<c:forEach items="${pouchCategory}" var="list">
						<form id="updatePouchCategory${list.idPouchCategory}" role="form" method="get" action="listPouchCategory.do"></form>
						<input type="hidden" name="id" value="${list.idPouchCategory}" form="updatePouchCategory${list.idPouchCategory}"/>
						<input type="hidden" name="page" value="${page.nowPage}" form="updatePouchCategory${list.idPouchCategory}"/>
						<c:if test="${order != null}">
							<input type="hidden" name="order" value="${fn:substringAfter(order, '=')}" form="updatePouchCategory${list.idPouchCategory}"/>
						</c:if>
						<tr class="text-center row">
							<td class="col-md-1">
								<input type="number" class="form-control" name="sortKey${list.idPouchCategory}" value="${list.sortKey}" form="updatePouchCategory${list.idPouchCategory}"/>
							</td>
							<td class="col-md-6">
								<input type="text" class="form-control" name="pouchCategoryText${list.idPouchCategory}" value="${list.pouchCategoryText}" form="updatePouchCategory${list.idPouchCategory}"/>
							</td>
							<td class="col-md-2">${list.pouchCnt}</td>
							<td class="col-md-1">
								<button type="submit" class="btn btn-warning btn-block btn-sm" form="updatePouchCategory${list.idPouchCategory}">수정완료</button>
							</td>
							<c:choose>
								<c:when test="${list.isDisplay == 1}">
									<td class="col-md-2">Y
										<a href="listPouchCategory.do?page=${page.nowPage}&id=${list.idPouchCategory}&isDisplay=${list.isDisplay}${order}">
											<button type="button" class="btn btn-xs btn-defalut unDisplay">삭제</button>
										</a>
									</td>
								</c:when>
								<c:otherwise>
									<td class="col-md-2">N
										<a href="listPouchCategory.do?page=${page.nowPage}&id=${list.idPouchCategory}&isDisplay=${list.isDisplay}${order}">
											<button type="button" class="btn btn-xs btn-primary display">승인</button>
										</a>
									</td>
								</c:otherwise>
							</c:choose>
						</tr>
					</c:forEach>
				</tbody>
			</table>
		</div>
		
		<div class="text-center">
			<ul class="pagination">

				<!-- 이전 그룹 -->
				<c:choose>
					<c:when test="${page.startPage > page.pageGroupCount}">
						<li><a href="listPouchCategory.do?page=${page.startPage-1}${order}">&laquo;</a></li>
					</c:when>
					<c:otherwise>
						<li class="disabled"><a href="#">&laquo;</a></li>
					</c:otherwise>
				</c:choose>
				
				<!-- 페이지 번호 -->
				<c:forEach begin="${page.startPage}" end="${page.endPage}" step="1" var="now">
					<c:choose>
						<c:when test="${page.nowPage == now}">
							<li class="active"><a href="#">${now}</a></li>
						</c:when>
						<c:otherwise>
							<li><a href="listPouchCategory.do?page=${now}${order}">${now}</a></li>
						</c:otherwise>
					</c:choose>
				</c:forEach>

				<!-- 다음 그룹 -->
				<c:choose>
					<c:when test="${page.endPage < page.lastGroupFirstPage}">
						<li><a href="listPouchCategory.do?page=${page.endPage+1}${order}">&raquo;</a></li>
					</c:when>
					<c:otherwise>
						<li class="disabled"><a href="#">&raquo;</a></li>
					</c:otherwise>
				</c:choose>

			</ul>
		</div>
		
	</div>
	<!-- Javascript -->
	<script src="assets/js/jquery.js"></script>
	<script src="assets/js/bootstrap.min.js"></script>
	<!-- 형식검사 클래스 참조 -->
	<script src="assets/js/string.js"></script>
	<script type="text/javascript">
		$(function() {
			$("#insertPouchCategory").submit(function() {
				
				if (!$("input[name='pouchCategoryText']").val().isValue()) {
					alert("카테고리명을 입력하세요.");
					$("input[name='pouchCategoryText']").focus();
					return false;
				}
				
				if (!$("input[name='sortKey']").val().isValue()) {
					alert("노출순서를 입력하세요.");
					$("input[name='sortKey']").focus();
					return false;
				}
				
				if (!$("input[name='sortKey']").val().isNumber()) {
					alert("노출순서는 숫자로만 입력이 가능합니다.");
					$("input[name='sortKey']").focus();
					return false;
				}
				
				if (!confirm("정말 등록하시겠습니까?")) {
					return false;
				}
			});
			
			<c:forEach items="${pouchCategory}" var="list">
				$("#updatePouchCategory${list.idPouchCategory}").submit(function() {
				
					if (!$("input[name='sortKey${list.idPouchCategory}']").val().isValue()) {
						alert("정렬순서를 입력하세요.");
						$("input[name='sortKey${list.idPouchCategory}']").focus();
						return false;
					}
					
					if (!$("input[name='sortKey${list.idPouchCategory}']").val().isNumber()) {
						alert("정렬순서는 숫자로만 입력이 가능합니다.");
						$("input[name='sortKey${list.idPouchCategory}']").focus();
						return false;
					}
					
					if (!$("input[name='pouchCategoryText${list.idPouchCategory}']").val().isValue()) {
						alert("카테고리 이름을 입력하세요.");
						$("input[name='pouchCategoryText${list.idPouchCategory}']").focus();
						return false;
					}
					
					if (!confirm("정말 수정하시겠습니까?")) {
						return false;
					}
				});
			</c:forEach>
		});
	</script>
	<script>
		$(function() {
			$("#searchdiv").hide();
			
			$("#opensearch").click(function() {
				$("#searchdiv").toggle();
				return false;
			});
			
			$(".unDisplay").click(function() {
				if (!confirm("정말 비활성처리 하시겠습니까?")) {
					return false;
				}
			});
			
			$(".display").click(function() {
				if (!confirm("정말 승인처리 하시겠습니까?")) {
					return false;
				}
			});
		});
	</script>
  </body>
</html>